/**
 * jims
 */
package com.jims.master.modules.nurse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.nurse.entity.NurseTempItemDict;
import com.jims.master.modules.nurse.service.NurseTempItemDictService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 护理项目字典Controller
 * @author CTQ
 * @version 2017-06-01
 */
@Controller
@RequestMapping(value = "${adminPath}/nurse/nurseTempItemDict")
public class NurseTempItemDictController extends BaseController {

	@Autowired
	private NurseTempItemDictService nurseTempItemDictService;
	
	@ModelAttribute
	public NurseTempItemDict get(@RequestParam(required=false) String id) {
		NurseTempItemDict entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = nurseTempItemDictService.get(id);
		}
		if (entity == null){
			entity = new NurseTempItemDict();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(NurseTempItemDict nurseTempItemDict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<NurseTempItemDict> page = nurseTempItemDictService.findPage(new Page<NurseTempItemDict>(request, response), nurseTempItemDict); 
		model.addAttribute("page", page);
		model.addAttribute("classCode", nurseTempItemDict.getClassCode());
		return "modules/nurse/nurseTempItemDictIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(NurseTempItemDict nurseTempItemDict, Model model) {
		model.addAttribute("nurseTempItemDict", nurseTempItemDict);
		return "modules/nurse/nurseTempItemDictForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(NurseTempItemDict nurseTempItemDict, Model model) {
	    StringData data=new StringData();
		int i=nurseTempItemDictService.save(nurseTempItemDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存护理项目字典成功");
        }else{
            data.setCode("error");
            data.setData("保存护理项目字典失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(NurseTempItemDict nurseTempItemDict) {
	    StringData data=new StringData();
		int i=nurseTempItemDictService.delete(nurseTempItemDict);
        if(i>0){
            data.setCode("success");
            data.setData("删除护理项目字典成功");
        }else{
            data.setCode("error");
            data.setData("删除护理项目字典失败");
        }
		return data;
	}

	/**
	 * 护理事件调用护理项目字典
	 * @param q
	 * @param limit
	 * @return
	 * @author CTQ
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"autoComplete", ""})
	@ResponseBody
	public List<NurseTempItemDict> autoComplete(String q,  String limit, String type) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
        // C 主要护理事件、D辅助护理事件
		return nurseTempItemDictService.findListByParam(q,limit, type, UserUtils.getOrgId(), UserUtils.getUser().getOrgDept().getId(), "D|C");
	}


}